g_object_name = '';
g_object_name_type = '';
key = "";
var policyText = {
    "expiration": "2020-01-01T12:00:00.000Z", //设置该Policy的失效时间，超过这个失效时间之后，就没有办法通过这个policy上传文件了
    "conditions": [
    ["content-length-range", 0, 1048576000] // 设置上传文件的大小限制
    ]
};

accessid = 'LTAI6Mxyu3msa9dk';
accesskey = 'Ml2W35ToZ3Y4Y2KLyqqbxrGEPIvbNf';
host = 'http://szbobaoge.oss-cn-beijing.aliyuncs.com';

var date = new Date();
var year = date.getFullYear();
var month = date.getMonth() + 1;
var strDate = date.getDate();
if (month >= 1 && month <= 9) {
    month = "0" + month;
}
if (strDate >= 0 && strDate <= 9) {
    strDate = "0" + strDate;
}
var currentdate = year + month + strDate;
var folder ="bobaoge"+ currentdate+"/";

var policyBase64 = Base64.encode(JSON.stringify(policyText));
message = policyBase64;
var bytes = Crypto.HMAC(Crypto.SHA1, message, accesskey, { asBytes: true }) ;
var signature = Crypto.util.bytesToBase64(bytes);
var uploader = new plupload.Uploader({
	runtimes : 'html5,flash,silverlight,html4',
	browse_button : 'file',
	flash_swf_url : '../lib/plupload-2.1.2/js/Moxie.swf',
	silverlight_xap_url : '../lib/plupload-2.1.2/js/Moxie.xap',

    url : host,

	multipart_params: {
		//'Filename':'bobaoge/' + '${filename}',
        'key' : folder + '${filename}',
		'policy': policyBase64,
        'OSSAccessKeyId': accessid, 
        'success_action_status' : '200', //让服务端返回200,不然，默认会返回204
		'signature': signature,
	},

	init: {
		PostInit: function() {
			document.getElementById('img-content').innerHTML = '';
			document.getElementById('btn-submit').onclick = function() {
                var message_content = $("#message_content").val().trim().length;
                if(message_content == 0){
                    alert("请输入爆款仓库内容内容！");
                }else {
                    var images = "";
                    $imgHtml = file.name;
                    $.each($imgHtml, function (n, value) {
                        images = images + value.src + "787311295";
                    });

                    var url = baseurl + "/?admin=message&func=addExplosion";
                    var explosion_type = $("#explosion_type").val();

                   var filename=document.getElementById("img-content").innerText;

                    $.post(url, {
                            "content": $("#message_content").val(),//消息内容
                            "images": filename,//图片HTML代码
                            "state": 1,//默认为启用状态
                            "explosion_type": explosion_type
                        },
                        function (data) {

                            if (data == 1) {
                                alert("发布成功");

                            } else {
                                alert("发布失败");
                            }
                        });

                }
                uploader.start();
                document.getElementById("btn-submit").disabled = true;
                return false;
			};
		},
        FilesAdded: function(up, files) {
            plupload.each(files, function(file) {
                document.getElementById('img-content').innerHTML += '<div id="'+file.id+'">'+file.name+'<b></b>'
                    +'<div class="progress"><div class="progress-bar" style="width: 0%"></div></div>'
                    +'</div>';
            });
        },

        UploadProgress: function(up, file) {
            var d = document.getElementById(file.id);
            d.getElementsByTagName('b')[0].innerHTML = '<span>' + file.percent + "%</span>";

            var prog = d.getElementsByTagName('div')[0];
            var progBar = prog.getElementsByTagName('div')[0]
            progBar.style.width= 2*file.percent+'px';
            progBar.setAttribute('aria-valuenow', file.percent);

        },
        Error: function(up, err) {
            document.getElementById('img-content').appendChild(document.createTextNode("\nError xml:" + err.response));
        }
	}
});

uploader.init();

